#include "StdAfx.h"
#include ".\mfcwpfelectrl.h"

CMFCWPFEleCtrl::CMFCWPFEleCtrl(void)
{
	m_pMFCWPFComboBox = NULL;
	m_pMFCWPFTextBox = NULL;
	m_pComboBox = NULL;
	m_pEdit = NULL;
	m_pFileDialog = NULL;
	m_fXRatio = 1.0;
	m_fYRatio = 1.0;
}

///<summary>
///Delete all the object
///</summary>
CMFCWPFEleCtrl::~CMFCWPFEleCtrl(void)
{
	if (m_pComboBox)
	{
		delete m_pComboBox;
		m_pComboBox = NULL;
	}
	if (m_pEdit)
	{
		delete m_pEdit;
		m_pEdit = NULL;
	}
	if (m_pFileDialog)
	{
		delete m_pFileDialog;
		m_pFileDialog = NULL;
	}
}

///<summary>
///According to the clicked element and the View window
///create and display the right control
///</summary>
///<param name='pMFCWPFElement'>
///The clicked element's pointer
///</param>
///<param name='pWnd'>
///The window's pointer where the click happened
///</param>
///<returns>
///true if clicked on some element or destroy happened; false nothing happened
///</returns>
bool CMFCWPFEleCtrl::MouseLeftButtonUp(CMFCWPFElement * pMFCWPFElement, CWnd * pWnd)
{
	if (pMFCWPFElement)
	{
		GetContentDestroyCtrl();
		CreateInitializeCtrl(pMFCWPFElement, pWnd);
		return true;
	}
	else
	{
		return GetContentDestroyCtrl();
	}
}

///<summary>
///According to different controls Get the control's content and destroy it
///</summary>
///<returns>
///true if destroy happened; false nothing happened
///</returns>
bool CMFCWPFEleCtrl::GetContentDestroyCtrl()
{
	CString szContent;
	if (m_pComboBox)
	{
		if (m_pComboBox->GetCurSel() != -1)
		{
			m_pComboBox->GetLBText(m_pComboBox->GetCurSel(), szContent);
			if (m_pMFCWPFComboBox)
			{
				m_pMFCWPFComboBox->SetContent(szContent);
			}
		}
		delete m_pComboBox;
		ClearElement();
		return true;
	}
	if (m_pEdit)
	{
		m_pEdit->GetWindowText(szContent);
		if (m_pMFCWPFTextBox)
		{
			m_pMFCWPFTextBox->SetContent(szContent);
		}
		delete m_pEdit;
		ClearElement();
		return true;
	}
	return false;
}

///<summary>
///Clear all the members
///</summary>
void CMFCWPFEleCtrl::ClearElement()
{
	m_pMFCWPFComboBox = NULL;
	m_pComboBox = NULL;
	m_pEdit = NULL;
	m_pMFCWPFTextBox = NULL;
}

///<summary>
///According to the clicked element and the View window
///create and display the right control
///</summary>
///<param name='pMFCWPFElement'>
///The clicked element's pointer
///</param>
///<param name='pWnd'>
///The window's pointer where the click happened
///</param>
void CMFCWPFEleCtrl::CreateInitializeCtrl(CMFCWPFElement * pMFCWPFElement, CWnd * pWnd)
{
	CString szType = pMFCWPFElement->GetElementType();
	if (szType == _ELEMENTCOMBOBOX)
	{
		CreateComboBox(pMFCWPFElement, pWnd);
	}
	if (szType == _ELEMENTTEXTBOX)
	{
		CreateTextEdit(pMFCWPFElement, pWnd);
	}
}

///<summary>
///According to the clicked element and the View window
///create and display the combobox control
///</summary>
///<param name='pMFCWPFElement'>
///The clicked element's pointer
///</param>
///<param name='pWnd'>
///The window's pointer where the click happened
///</param>
void CMFCWPFEleCtrl::CreateComboBox(CMFCWPFElement * pMFCWPFElement, CWnd * pWnd)
{
	m_pMFCWPFComboBox = static_cast<CMFCWPFComboBox *>(pMFCWPFElement);
	m_pComboBox = new CComboBox;
	m_pComboBox->Create(WS_CHILD|WS_VISIBLE|WS_VSCROLL|CBS_DROPDOWNLIST, m_pMFCWPFComboBox->GetRect(), pWnd, 1);
	vector<CString> szVec;
	if (m_pMFCWPFComboBox->GetItems(szVec))
	{
		for (vector<CString>::iterator iter = szVec.begin();
			iter != szVec.end();
			++iter)
		{
			m_pComboBox->AddString(*iter);
		}
		m_pComboBox->SetCurSel(m_pComboBox->FindString(0, m_pMFCWPFComboBox->GetContent()));
	}
	ResetCtrlRect();
	m_pComboBox->ShowWindow(SW_SHOW);
}

///<summary>
///According to the clicked element and the View window
///create and display the edit control
///</summary>
///<param name='pMFCWPFElement'>
///The clicked element's pointer
///</param>
///<param name='pWnd'>
///The window's pointer where the click happened
///</param>
void CMFCWPFEleCtrl::CreateTextEdit(CMFCWPFElement * pMFCWPFElement, CWnd * pWnd)
{
	m_pMFCWPFTextBox = static_cast<CMFCWPFTextBox *>(pMFCWPFElement);
	m_pEdit = new CEdit;
	m_pEdit->Create(WS_CHILD|WS_BORDER|WS_VISIBLE, m_pMFCWPFTextBox->GetRect(), pWnd, 1);
	m_pEdit->SetWindowText((LPCTSTR)m_pMFCWPFTextBox->GetContent());
	m_pEdit->SetFocus();
	ResetCtrlRect();
	m_pEdit->ShowWindow(SW_SHOW);
}

///<summary>
///According to the ratio move the control to the right place
///</summary>
void CMFCWPFEleCtrl::ResetCtrlRect()
{
	if (m_pComboBox)
	{
		CRect m_oRect = m_pMFCWPFComboBox->GetRect();
		CRect oRect((int)(m_oRect.left*m_fXRatio), (int)(m_oRect.top*m_fYRatio), (int)(m_oRect.right*m_fXRatio), (int)(m_oRect.top*m_fYRatio+m_oRect.Height()));
		m_pComboBox->MoveWindow(oRect, true);
	}
	if (m_pEdit)
	{
		CRect m_oRect = m_pMFCWPFTextBox->GetRect();
		CRect oRect((int)(m_oRect.left*m_fXRatio), (int)(m_oRect.top*m_fYRatio), (int)(m_oRect.right*m_fXRatio+1), (int)(m_oRect.bottom*m_fYRatio+1));
		m_pEdit->MoveWindow(oRect, true);
	}
}